import { parse } from 'qs'
import modelExtend from 'dva-model-extend'
import api from 'api'
import { pathMatchRegexp } from 'utils'
import { model } from 'utils/model'
import { message } from 'antd'

const { queryDashboard, queryWeather, operatedashboard } = api

export default modelExtend(model, {
  namespace: 'dashboard',
  state: {
    data: {},
    // weather: {
    //   city: '上海',
    //   temperature: '30',
    //   name: '晴',
    //   icon: '//s5.sencdn.com/web/icons/3d_50/2.png',
    // },
    // sales: [],
    // quote: {
    //   avatar:
    //     'http://img.hb.aicdn.com/bc442cf0cc6f7940dcc567e465048d1a8d634493198c4-sPx5BR_fw236',
    // },
    // numbers: [],
    // recentSales: [],
    // comments: [],
    // completed: [],
    // browser: [],
    // cpu: {},
    // user: {
    //   avatar:
    //     'http://img.hb.aicdn.com/bc442cf0cc6f7940dcc567e465048d1a8d634493198c4-sPx5BR_fw236',
    // },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (
          pathMatchRegexp('/dashboard', pathname) ||
          pathMatchRegexp('/', pathname)
        ) {
          dispatch({ type: 'query' })
          // dispatch({ type: 'queryWeather' })
        }
      })
    },
  },
  effects: {
    *query({ payload }, { call, put }) {
      const { result } = yield call(queryDashboard, payload)
      yield put({
        type: 'updateState',
        payload: {
          data: result,
        },
      })
    },
    *operatedashboard({ payload }, { call, put }) {
      message.loading('修改首页中...', 0)
      yield call(operatedashboard, payload)
      message.destroy()
      message.success('修改成功！')
    },
    // *queryWeather({ payload = {} }, { call, put }) {
    //   payload.location = 'shenzhen'
    //   // const result = yield call(queryWeather, payload)
    //   const { success } = {}//result
    //   if (success) {
    //     const data = result.results[0]
    //     const weather = {
    //       city: data.location.name,
    //       temperature: data.now.temperature,
    //       name: data.now.text,
    //       icon: `//s5.sencdn.com/web/icons/3d_50/${data.now.code}.png`,
    //     }
    //     yield put({
    //       type: 'updateState',
    //       payload: {
    //         weather,
    //       },
    //     })
    //   }
    // },
  },
  reducer: {
    queryuserssuccess(state, { payload }) {
      state.modelusers = payload
    },
  },
})
